package com.example.gokuplayalong.service.privateclient;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.alipay.AlipayWork;
import com.example.gokuplayalong.bases.base.ExceptionHandler;
import com.example.gokuplayalong.bases.base.Tools;
import com.example.gokuplayalong.bases.mysql.MysqlDruid;
import com.example.gokuplayalong.bases.push.PublishController;
import io.micrometer.common.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Pattern;

public class PrivateClient {
    public Map<String, Object> Client(JSONObject msg) {
        Map<String, Object> msgToClient = new HashMap<>();
        JSONObject bean = JSON.parseObject(msg.getString("data"));
        bean.put("name", msg.getString("name"));

        System.out.println("收到:" + bean.toJSONString());
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
        if (conn != null && bean != null && !StringUtils.isEmpty(bean.getString("name"))) {
            msgToClient.put("name", bean.getString("name"));
            if (Objects.equals(bean.getString("name"), "login")) {
                try {
                    pstmt = conn.prepareStatement("select * from admininfo");
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        if (Objects.equals(rs.getString("username"), bean.getString("username"))) {
                            if (Objects.equals(rs.getString("password"), bean.getString("password"))) {
                                String token = Tools.getOrderNum();
                                msgToClient.put("username", bean.get("username"));
                                msgToClient.put("status", 200);
                                msgToClient.put("message", "登录成功");
                                msgToClient.put("token", token);
                                pstmt = conn.prepareStatement("update admininfo set token=\"" + token + "\"");
                                pstmt.executeUpdate();
                            }
                        }
                    }
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "orderlist")) {
                try {
                    List<Map<String, Object>> orderlist = new ArrayList<>();
                    pstmt = conn.prepareStatement("select * from orderlist ");
                    rs = pstmt.executeQuery();
                    orderlist = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", orderlist);
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

                }
            }
            else if (Objects.equals(bean.getString("name"), "users")) {
                List<Map<String, Object>> users = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from profileinfo");
                    rs = pstmt.executeQuery();
                    users = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", users);
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "audits")) {
                List<Map<String, Object>> audits = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from auditslist");
                    rs = pstmt.executeQuery();
                    audits = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", audits);
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }

            }
            else if (Objects.equals(bean.getString("name"), "reviews")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from review where status=\"待审核\" and type!=\"举报\" and type=\"提现申请\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "orderreviews")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from gkcancelorder where status=\"待审核\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "merchantsreviews")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from merchants where status=\"待审核\" or status=\"待签约\" order by timestamp desc");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "merchantsreviewsall")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from merchants order by timestamp desc");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "merchantsCreateFinish")) {

                try {

                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("update merchants set servicetype='{\"游戏陪玩\":30}',merchantid=\"" + bean.getString("merchantId") + "\",status=\"审核通过\" where phone=\"" + bean.getString("phone") + "\" ");
                    pstmt.executeUpdate();
                    pstmt = conn.prepareStatement("update profileinfo set merchantcertify=1 where phone=\"" + bean.getString("phone") + "\" ");
                    pstmt.executeUpdate();
                    conn.commit();
                } catch (Throwable e) {
                    try {
                        conn.rollback();
                        throw new RuntimeException(e);

                    } catch (Throwable e2) {
                        throw new RuntimeException(e2);

                    }
                }

                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from merchants where status=\"待审核\" or status=\"待签约\" order by timestamp desc");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "merchantsCreatePass")) {

                try {

                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("update merchants set status=\"待签约\" where phone=\"" + bean.getString("phone") + "\" ");
                    pstmt.executeUpdate();
                    conn.commit();
                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (Throwable e2) {
                    }
                }

                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from merchants where status=\"待审核\" or status=\"待签约\" order by timestamp desc");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "merchantsCreateUnpass")) {

                try {

                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("update merchants set status=\"审核不通过:" + bean.getString("unpassReason") + "\" where phone=\"" + bean.getString("phone") + "\" ");
                    pstmt.executeUpdate();
                    conn.commit();
                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (Throwable e2) {
                    }
                }
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from merchants where status=\"待审核\" or status=\"待签约\" order by timestamp desc");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "historyreviews")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from review where status!=\"待审核\" and type!=\"举报\"");

                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "reports")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from review where type=\"举报\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "monitors")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from sysmonitor order by timestamp desc ");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "exceptions")) {
                try {
                    List<Map<String, Object>> reviews = new ArrayList<>();

                    pstmt = conn.prepareStatement("select * from exceptionslist where exceptionstatus=\"待审核\" ");
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        Map<String, Object> item = new JSONObject();
                        item = new JSONObject(Tools.getResultMap(rs));

                        pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + rs.getString("ordernum") + "\"");
                        ResultSet rsOrder = pstmt.executeQuery();
                        if (rsOrder.next()) {
                            item.put("type", rsOrder.getString("type"));
                            item.put("exception", rsOrder.getString("exception"));
                            item.put("exceptionback", rsOrder.getString("exceptionback"));
                        }

                        reviews.add(item);
                    }
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "solved")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {

                    pstmt = conn.prepareStatement("update exceptionslist set exceptionstatus=\"已解决\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                    pstmt.executeUpdate();

                    pstmt = conn.prepareStatement("update orderlist set runningstatus=\"正常\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                    pstmt.executeUpdate();

                    pstmt = conn.prepareStatement("select * from exceptionslist where exceptionstatus!=\"已解决\";");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);

                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "alipays")) {
                List<Map<String, Object>> alipays = new ArrayList<>();
                try {


//                    pstmt = conn.prepareStatement("select * from exceptionslist");
//                    rs = pstmt.executeQuery();
//                    reviews = Tools.convertList(rs);
//
//
//
//                    msgToClient.put("status", 200);
//                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "payoutsPass")) {
                try {
                    conn.setAutoCommit(false);
                    List<Map<String, Object>> reviews = new ArrayList<>();
                    msgToClient.put("status", 400);
                    pstmt = conn.prepareStatement("select * from review where rand=\"" + bean.getString("rand") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        if (Objects.equals(bean.getString("type"), "提现申请") && Objects.equals(rs.getString("status"), "待审核")) {
                            pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                            ResultSet rsRealname = pstmt.executeQuery();
                            if (rsRealname.next()) {
                                bean.put("realname", rsRealname.getString("realname"));
                            }
                            bean.put("out_trade_no", Tools.getOutTradeNo());
                            if (AlipayWork.payoutAlipay(bean) == 1) {
                                pstmt = conn.prepareStatement("update review set status=\"已打款\" where rand=\"" + bean.getString("rand") + "\"");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + bean.getString("rand") + "\",\"" + bean.getString("phone") + "\",\"提现成功\",'{\"msg\":\"消息内容：您申请的提现申请审核通过，提现金额: " + Math.abs(Tools.doubleFormat(bean.getDoubleValue("var"))) + "元， 金额已到账，请查收，如有问题请联系在线客服。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("update balancemonitor set note=\"提现-已打款\" where rand=\"" + bean.getString("rand") + "\"");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("insert into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"提现-操作打款-管理员\",\"log\")");
                                pstmt.executeUpdate();
                                msgToClient.put("status", 200);

                                new PublishController().pushMessageToOne(bean.getString("phone"), new JSONArray().toJSONString());

                            }
                            else {

                                pstmt = conn.prepareStatement("insert into sysmonitor(timestamp,msgtime, msg, msglevel) values(" + System.currentTimeMillis() + ",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"提现-提现失败-" + bean.getString("phone") + "-" + bean.getString("out_trade_no") + "-" + bean.getString("response") + "\",\"error\")");
                                pstmt.executeUpdate();
                            }

                        }
                    }
                    pstmt = conn.prepareStatement("select * from review where status=\"待审核\" and type=\"提现申请\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("data", reviews);

                    conn.commit();
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

                }
            }
            else if (Objects.equals(bean.getString("name"), "payoutsUnpass")) {
                try {
                    List<Map<String, Object>> reviews = new ArrayList<>();
                    msgToClient.put("status", 400);
                    pstmt = conn.prepareStatement("select * from review where rand=\"" + bean.getString("rand") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        if (Objects.equals(bean.getString("type"), "提现申请") && Objects.equals(rs.getString("status"), "待审核")) {
                            conn.setAutoCommit(false);

                            JSONObject note = JSON.parseObject(rs.getString("note"));
                            note.put("unpassReason", bean.getString("unpassReason"));
                            pstmt = conn.prepareStatement("update review set status=\"不通过\",note='" + note.toJSONString() + "' where rand=\"" + bean.getString("rand") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=payoutsbalance+" + Math.abs(Tools.doubleFormat(bean.getDoubleValue("var"))) + " where phone=\"" + bean.getString("phone") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("update balancemonitor set note=\"提现-审核不通过\",currentbalance=currentbalance+" + Math.abs(Tools.doubleFormat(bean.getDoubleValue("var"))) + " where rand=\"" + bean.getString("rand") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + bean.getString("rand") + "\",\"" + bean.getString("phone") + "\",\"提现失败\",'{\"msg\":\"消息内容：您申请的提现申请审核失败。失败原因：" + bean.getString("unpassReason") + " 。如有问题请联系在线客服。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                            pstmt.executeUpdate();

                            conn.commit();
                            msgToClient.put("status", 200);

                            new PublishController().pushMessageToOne(bean.getString("phone"), new JSONArray().toJSONString());

                        }
                    }
                    pstmt = conn.prepareStatement("select * from review where status=\"待审核\" and type=\"提现申请\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {

                }
            }
            else if (Objects.equals(bean.getString("name"), "receiverreviews")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    try {
                        pstmt = conn.prepareStatement("select * from review where status=\"待审核\" and type=\"打手认证\"");
                        rs = pstmt.executeQuery();
                        reviews = Tools.convertList(rs);
                        msgToClient.put("status", 200);
                        msgToClient.put("data", reviews);

                    } catch (Throwable e) {
                        ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                    }
                } catch (Throwable e) {

                }

            }
            else if (Objects.equals(bean.getString("name"), "receiverCertifyPass")) {
                try {
                    conn.setAutoCommit(false);
                    JSONObject idcertify = new JSONObject();
                    pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        if (!StringUtils.isEmpty(rs.getString("idcertify"))) {
                            idcertify = JSON.parseObject(rs.getString("idcertify"));
                        }
                    }
                    idcertify.put("receiveCertifyResult", "打手认证成功");
                    idcertify.put("receiveCertifyStatus", "已通过");
                    pstmt = conn.prepareStatement("update profileinfo set idcertify='" + idcertify.toJSONString() + "' where phone=\"" + bean.getString("phone") + "\"");
                    pstmt.executeUpdate();
                    pstmt = conn.prepareStatement("update review set status=\"已通过\" where rand=\"" + bean.getString("rand") + "\"");
                    pstmt.executeUpdate();
                    pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + bean.getString("phone") + "\",\"认证成功\",'{\"msg\":\"消息内容：打手认证成功！\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                    pstmt.executeUpdate();

                    JSONObject taskstep = new JSONObject();
                    pstmt = conn.prepareStatement("select * from newusertask where phone=\"" + bean.getString("phone") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        if (!StringUtils.isEmpty(rs.getString("taskstep"))) {
                            taskstep = JSON.parseObject(rs.getString("taskstep"));
                        }
                    }
                    if (taskstep.getIntValue("task2") == 0) {
                        taskstep.put("task2", 1);

                    }

                    pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        if (Objects.equals(rs.getString("identity"), "接单")) {
                            pstmt = conn.prepareStatement("update newusertask set taskstep='" + taskstep.toJSONString() + "' where phone=\"" + bean.getString("phone") + "\"");
                            pstmt.executeUpdate();
                        }
                    }


                    conn.commit();

                    JSONArray jsonArray = new JSONArray();
                    JSONObject item = new JSONObject();
                    item.put("key", "title");
                    item.put("value", "打手认证成功");
                    jsonArray.add(item);
                    item = new JSONObject();
                    item.put("key", "type");
                    item.put("value", "系统通知");
                    jsonArray.add(item);
                    new PublishController().pushMessageToOne(bean.getString("phone"), jsonArray.toJSONString());

                    pstmt = conn.prepareStatement("select * from review where type=\"打手认证\" and status=\"待审核\"");
                    rs = pstmt.executeQuery();

                    List<Map<String, Object>> reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (SQLException ex) {

                    }
                }
            }
            else if (Objects.equals(bean.getString("name"), "receiverCertifyUnpass")) {
                try {
                    conn.setAutoCommit(false);
                    JSONObject idcertify = new JSONObject();
                    pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        if (!StringUtils.isEmpty(rs.getString("idcertify"))) {
                            idcertify = JSON.parseObject(rs.getString("idcertify"));

                        }
                    }
                    idcertify.put("receiveCertifyResult", "认证不通过");

                    idcertify.put("receiveCertifyUnpassReason", bean.getString("unpassReason"));
                    idcertify.put("receiveCertifyStatus", "不通过");
                    pstmt = conn.prepareStatement("update profileinfo set idcertify='" + idcertify.toJSONString() + "' where phone=\"" + bean.getString("phone") + "\"");
                    pstmt.executeUpdate();
                    pstmt = conn.prepareStatement("update review set status=\"不通过\" where rand=\"" + bean.getString("rand") + "\"");
                    pstmt.executeUpdate();

                    conn.commit();

                    pstmt = conn.prepareStatement("select * from review where type=\"打手认证\" and status=\"待审核\"");
                    rs = pstmt.executeQuery();

                    List<Map<String, Object>> reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                    JSONArray jsonArray = new JSONArray();
                    JSONObject item = new JSONObject();
                    item.put("key", "title");
                    item.put("value", "打手认证失败");
                    jsonArray.add(item);
                    item = new JSONObject();
                    item.put("key", "type");
                    item.put("value", "系统通知");
                    jsonArray.add(item);
                    new PublishController().pushMessageToOne(bean.getString("phone"), jsonArray.toJSONString());

                } catch (Throwable e) {
                }
            }
            else if (Objects.equals(bean.getString("name"), "logoutreviews")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from review where status=\"待审核\"  and type=\"注销审核\"");
                    rs = pstmt.executeQuery();

                    while (rs.next()) {
                        if (Tools.addVipDatedTime(Long.parseLong(rs.getString("timestamp")), 0, 0, 14) < System.currentTimeMillis()) {
                            reviews.add(new JSONObject(Tools.getResultMap(rs)));
                        }
                    }

                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "sysbalances")) {
                List<Map<String, Object>> reviews = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from sysbalance");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }

            }
            else if (Objects.equals(bean.getString("name"), "getUserList")) {
                JSONArray usersList = new JSONArray();

                try {


                    pstmt = conn.prepareStatement("select DISTINCT send from matchchat where send!=\"13888888888\" order by msgid desc ");
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        JSONObject item = new JSONObject();
                        pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("send") + "\"");
                        ResultSet rsProfile = pstmt.executeQuery();
                        if (rsProfile.next()) {
                            item.putAll(Tools.getResultMap(rsProfile));
                        }

                        pstmt = conn.prepareStatement("select count(*) from matchchat where msgStatus=\"未读\" and send=\"" + rs.getString("send") + "\" and receive=\"13888888888\"");
                        ResultSet rsCount = pstmt.executeQuery();
                        if (rsCount.next()) {
                            item.put("count", rsCount.getInt(1));
                        }

                        usersList.add(item);


                    }
                    msgToClient.put("userList", usersList.toJSONString());
                    msgToClient.put("result", "成功");


                } catch (Throwable e) {

                }
            }
            else if (Objects.equals(bean.getString("name"), "getRecentlyUserList")) {

                try {


                    msgToClient.put("result", "成功");


                } catch (Throwable e) {

                }
            }
            else if (Objects.equals(bean.getString("name"), "getChatPageList")) {
                JSONArray chatList = new JSONArray();
                try {
                    pstmt = conn.prepareStatement("select * from (select * from (select * from matchchat where (send=\"" + bean.getString("phone") + "\" and receive=\"" + bean.getString("chatObject") + "\" ) or (send=\"" + bean.getString("chatObject") + "\" and receive=\"" + bean.getString("phone") + "\"))matchchat order by msgId desc limit " + bean.getIntValue("chatNum") * bean.getIntValue("chatCount") + ")a order by msgId");
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        chatList.add(new JSONObject(Tools.getResultMap(rs)));
                    }
                    msgToClient.put("chatList", chatList.toJSONString());
                    msgToClient.put("result", "成功");

                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("update matchchat set msgStatus=\"已读\" where msgStatus=\"未读\" and  (send=\"" + bean.getString("chatObject") + "\" and receive=\"" + bean.getString("phone") + "\")");
                    pstmt.executeUpdate();
                    conn.commit();


                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (Throwable ex) {

                    }
                }
            }
            else if (Objects.equals(bean.getString("name"), "cancelOrderPass")) {
                try {
                    JSONObject cancelReason = JSON.parseObject(bean.getString("cancelreason"));

                    double bondLevel = cancelReason.getDoubleValue("bondLevel");
                    //{"cancelreason":"{\"type\": \"取消订单\", \"bondLevel\": 0.5, \"imageList\": \"13651178942_20241104215905_cancel_order_0.png\", \"cancelreason\": \"jdkdoo\"}","name":"cancelOrderPass","ordernum":"20241104215905","timestamp":"1730728857244","status":"待审核"}
                    conn.setAutoCommit(false);


                    pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + bean.getString("ordernum") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        pstmt = conn.prepareStatement("update orderlist set runningstatus=\"正常\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                        pstmt.executeUpdate();

                        if (Objects.equals(cancelReason.getString("cancelType"), "取消订单")) {
                            //修改接单者
                            double balance = 0;
                            double payoutsbalance = 0;
                            double unpayoutsbalance = 0;
                            double receivebondunpayoutsbalance = rs.getDouble("receivebondunpayoutsbalance");
                            double receivebondpayoutsbalance = rs.getDouble("receivebondpayoutsbalance");
                            double bond = rs.getDouble("receivebondunpayoutsbalance") + rs.getDouble("receivebondpayoutsbalance");
                            double bondCancel = bond * bondLevel;

                            receivebondunpayoutsbalance -= bondCancel;
                            if (receivebondunpayoutsbalance < 0) {
                                receivebondpayoutsbalance += receivebondunpayoutsbalance;

                                receivebondunpayoutsbalance = 0;
                            }


                            pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("receivephone") + "\"");
                            ResultSet rsProfile = pstmt.executeQuery();
                            if (rsProfile.next()) {
                                payoutsbalance = rsProfile.getDouble("payoutsbalance");
                                unpayoutsbalance = rsProfile.getDouble("unpayoutsbalance");
                            }

                            payoutsbalance += receivebondpayoutsbalance;
                            unpayoutsbalance += receivebondunpayoutsbalance;

                            balance = payoutsbalance + unpayoutsbalance;

                            pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=" + Tools.doubleFormat(payoutsbalance) + ",unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + rs.getString("receivephone") + "\"");
                            pstmt.executeUpdate();

                            pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("receivephone") + "\",\"取消订单\",'{\"msg\":\"消息内容：订单编号为" + rs.getString("ordernum") + "订单已被取消，取消结果：扣除保证金" + Tools.doubleFormat(bondCancel) + "元。如有问题，请联系在线客服!\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                            pstmt.executeUpdate();

                            pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis() - 1) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=" + Tools.doubleFormat(-(1.0 - bondLevel) * bond) + ",note=\"归还保证金-订单取消" + bean.getString("ordernum") + "\" where note=\"冻结保证金" + bean.getString("ordernum") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("insert into balancemonitor(rand,currentbalance,timestamp,phone, var, note, time) values(\"" + Tools.getOrderNum() + "\"," + Tools.doubleFormat(balance) + "," + System.currentTimeMillis() + ",\"" + rs.getString("receivephone") + "\"," + Tools.doubleFormat(-bondCancel) + ",\"扣除保证金-订单取消" + bean.getString("ordernum") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
                            pstmt.executeUpdate();


                            //修改发单者
                            pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("phone") + "\"");
                            rsProfile = pstmt.executeQuery();
                            if (rsProfile.next()) {
                                payoutsbalance = rsProfile.getDouble("payoutsbalance");
                                unpayoutsbalance = rsProfile.getDouble("unpayoutsbalance");
                            }

                            payoutsbalance += bondCancel;

                            balance = payoutsbalance + unpayoutsbalance;


                            pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=" + Tools.doubleFormat(payoutsbalance) + ",unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + rs.getString("phone") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("phone") + "\",\"订单取消补偿\",'{\"msg\":\"消息内容：您发布的订单编号为" + rs.getString("ordernum") + "订单已取消，订单补偿" + Tools.doubleFormat(bondCancel) + "元已到账。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("insert into balancemonitor(rand,currentbalance,timestamp,phone, var, note, time) values(\"" + Tools.getOrderNum() + "\"," + Tools.doubleFormat(balance) + "," + System.currentTimeMillis() + ",\"" + rs.getString("phone") + "\"," + Tools.doubleFormat(bondCancel) + ",\"订单取消补偿" + bean.getString("ordernum") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
                            pstmt.executeUpdate();

                            pstmt = conn.prepareStatement("update orderlist set status=\"待接手\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("delete from gkflow where ordernum=\"" + bean.getString("ordernum") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("insert into gkflow(ordernum, status, note, noteTime, timestamp) values(\"" + bean.getString("ordernum") + "\",\"待接手\",\"\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\"," + System.currentTimeMillis() + ")");
                            pstmt.executeUpdate();

                        }
                        else if (Objects.equals(cancelReason.getString("cancelType"), "审核不通过")) {

                            //修改订单
                            pstmt = conn.prepareStatement("update orderlist set status=\"不通过\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                            pstmt.executeUpdate();
                            pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + bean.getString("ordernum") + "\"");
                            rs = pstmt.executeQuery();
                            if (rs.next()) {
                                //修改接单者
                                double balance = 0;
                                double payoutsbalance = 0;
                                double unpayoutsbalance = 0;
                                double receivebondunpayoutsbalance = rs.getDouble("receivebondunpayoutsbalance");
                                double receivebondpayoutsbalance = rs.getDouble("receivebondpayoutsbalance");
                                double bond = rs.getDouble("receivebondunpayoutsbalance") + rs.getDouble("receivebondpayoutsbalance");
                                double bondCancel = bond * bondLevel;

                                receivebondunpayoutsbalance -= bondCancel;
                                if (receivebondunpayoutsbalance < 0) {
                                    receivebondpayoutsbalance += receivebondunpayoutsbalance;

                                    receivebondunpayoutsbalance = 0;
                                }


                                pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("receivephone") + "\"");
                                ResultSet rsProfile = pstmt.executeQuery();
                                if (rsProfile.next()) {
                                    payoutsbalance = rsProfile.getDouble("payoutsbalance");
                                    unpayoutsbalance = rsProfile.getDouble("unpayoutsbalance");
                                }

                                payoutsbalance += receivebondpayoutsbalance;
                                unpayoutsbalance += receivebondunpayoutsbalance;

                                balance = payoutsbalance + unpayoutsbalance;

                                pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=" + Tools.doubleFormat(payoutsbalance) + ",unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + rs.getString("receivephone") + "\"");
                                pstmt.executeUpdate();

                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("receivephone") + "\",\"审核不通过\",'{\"msg\":\"消息内容：订单编号为" + rs.getString("ordernum") + "订单审核不通过，审核结果：经系统审核，订单存在问题，扣除保证金" + Tools.doubleFormat(bondCancel) + "元。如有疑问，请联系在线客服!\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis() - 1) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=" + Tools.doubleFormat((1.0 - bondLevel) * bond) + ",note=\"归还保证金-订单不通过" + bean.getString("ordernum") + "\" where note=\"冻结保证金" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("insert into balancemonitor(rand,currentbalance,timestamp,phone, var, note, time) values(\"" + Tools.getOrderNum() + "\"," + Tools.doubleFormat(balance) + "," + System.currentTimeMillis() + ",\"" + rs.getString("receivephone") + "\"," + Tools.doubleFormat(-bondCancel) + ",\"扣除保证金-订单不通过" + bean.getString("ordernum") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
                                pstmt.executeUpdate();


                                //修改发单者
                                pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("phone") + "\"");
                                rsProfile = pstmt.executeQuery();
                                if (rsProfile.next()) {
                                    payoutsbalance = rsProfile.getDouble("payoutsbalance");
                                    unpayoutsbalance = rsProfile.getDouble("unpayoutsbalance");
                                }

                                double publishunpayoutsbalance = rs.getDouble("publishunpayoutsbalance");
                                double publishpayoutsbalance = rs.getDouble("publishpayoutsbalance");

                                payoutsbalance += publishpayoutsbalance;
                                unpayoutsbalance += publishunpayoutsbalance;

                                payoutsbalance += bondCancel;

                                balance = payoutsbalance + unpayoutsbalance;


                                pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=" + Tools.doubleFormat(payoutsbalance) + ",unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + rs.getString("phone") + "\"");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("phone") + "\",\"订单取消补偿\",'{\"msg\":\"消息内容：您发布的订单编号为" + rs.getString("ordernum") + "订单补偿" + Tools.doubleFormat(bondCancel) + "元已到账。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("insert into balancemonitor(rand,currentbalance,timestamp,phone, var, note, time) values(\"" + Tools.getOrderNum() + "\"," + Tools.doubleFormat(balance) + "," + (System.currentTimeMillis() - 1) + ",\"" + rs.getString("phone") + "\"," + Tools.doubleFormat(bondCancel) + ",\"订单取消补偿" + bean.getString("ordernum") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
                                pstmt.executeUpdate();
                                pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis()) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=-var,note=\"归还冻结金额-订单不通过" + bean.getString("ordernum") + "\" where note=\"冻结-创建订单" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();

                                pstmt = conn.prepareStatement("insert into gkflow(ordernum, status, note, noteTime, timestamp) values(\"" + bean.getString("ordernum") + "\",\"不通过\",\"订单不通过，订单结束。\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\"," + System.currentTimeMillis() + ")");
                                pstmt.executeUpdate();

                            }


                        }


                    }
                    pstmt = conn.prepareStatement("update gkcancelorder set status=\"已处理\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                    pstmt.executeUpdate();

                    conn.commit();

                    List<Map<String, Object>> reviews = new ArrayList<>();

                    pstmt = conn.prepareStatement("select * from gkcancelorder where status=\"待审核\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

                    try {
                        conn.rollback();
                    } catch (Throwable ex) {
                        ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

                    }
                }
            }
            else if (Objects.equals(bean.getString("name"), "cancelOrderUnpass")) {
                try {
                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + bean.getString("ordernum") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {


                        JSONObject data = JSON.parseObject(rs.getString("data"));
                        data.put("cancelOrderUnpassReason", bean.getString("unpassReason"));
                        pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("phone") + "\",\"审核未通过\",'{\"msg\":\"消息内容：订单编号为" + rs.getString("ordernum") + "的订单申请扣除保证金取消，经过平台审核不通过，原因：" + bean.getString("unpassReason") + "，请修改后重新提交。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                        pstmt.executeUpdate();
                        pstmt = conn.prepareStatement("update orderlist set data='" + data.toJSONString() + "' where ordernum=\"" + bean.getString("ordernum") + "\"");
                        pstmt.executeUpdate();

                    }

                    conn.commit();
                    pstmt = conn.prepareStatement("update gkcancelorder set status=\"已处理\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                    pstmt.executeUpdate();
                    List<Map<String, Object>> reviews = new ArrayList<>();

                    pstmt = conn.prepareStatement("select * from gkcancelorder where status=\"待审核\"");
                    rs = pstmt.executeQuery();
                    reviews = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);


                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

                    try {
                        conn.rollback();
                    } catch (Throwable ex) {
                        ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);

                    }
                }
            }
            else if (Objects.equals(bean.getString("name"), "logoutPass")) {
                try {
                    //注销通过
                } catch (Throwable e) {

                }
            }
            else if (Objects.equals(bean.getString("name"), "exceptionBackApi")) {
                try {
                    JSONObject exception = new JSONObject();
                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + bean.getString("ordernum") + "\"");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        exception = JSON.parseObject(rs.getString("exception"));
                    }
                    exception.put("backReason", bean.getString("backReason"));
                    pstmt = conn.prepareStatement("update orderlist set exception='" + exception.toJSONString() + "' where ordernum=\"" + bean.getString("ordernum") + "\"");
                    pstmt.executeUpdate();

                    conn.commit();
                    msgToClient.put("result", "成功");
                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (SQLException ex) {
                    }
                }
                try {
                    List<Map<String, Object>> reviews = new ArrayList<>();

                    pstmt = conn.prepareStatement("select * from exceptionslist where exceptionstatus=\"待审核\"   ");
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        Map<String, Object> item = new JSONObject();
                        item = new JSONObject(Tools.getResultMap(rs));

                        pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + rs.getString("ordernum") + "\"");
                        ResultSet rsOrder = pstmt.executeQuery();
                        if (rsOrder.next()) {
                            item.put("type", rsOrder.getString("type"));
                            item.put("exception", rsOrder.getString("exception"));
                            item.put("exceptionback", rsOrder.getString("exceptionback"));
                        }

                        reviews.add(item);
                    }
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }

            }
            else if (Objects.equals(bean.getString("name"), "exceptionFinishProcessApi")) {
                try {
                    String NUMBER_REGEX_JUDGE = "^((\\d|[123456789]\\d)(\\.\\d+)?|100)$";
                    String DEFAULT_REGEX = "[^\\u4E00-\\u9FA50-9a-zA-Z,，.。！!?？@\n]";


                    if (!Pattern.matches(NUMBER_REGEX_JUDGE, bean.getString("processPercent"))) {
                        msgToClient.put("result", "代练进度格式不正确");
                    }
                    else if (!Pattern.matches(NUMBER_REGEX_JUDGE, bean.getString("bondIncPercent"))) {
                        msgToClient.put("result", "保证金比例格式不正确");
                    }
                    else {

                        conn.setAutoCommit(false);
                        pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + bean.getString("ordernum") + "\"");
                        rs = pstmt.executeQuery();
                        if (rs.next()) {


                            JSONObject data = JSON.parseObject(rs.getString("data"));

                            double balance = 0;
                            double payoutsbalance = 0;
                            double unpayoutsbalance = 0;
                            double processPercent = Double.parseDouble(bean.getString("processPercent"));
                            double bondIncPercent = Double.parseDouble(bean.getString("bondIncPercent"));
                            String finishDesc = bean.getString("finishDesc");


                            //本订单扣除的保证金 发单者加 接单者减
                            double bondInc = data.getDoubleValue("bond") * bondIncPercent / 100.0;
                            //本订单结算的报酬 发单者减 接单者加
                            double orderBalanceAdd = data.getDoubleValue("balance") * processPercent / 100.0;

                            //修改发单者

                            pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("phone") + "\"");
                            ResultSet rsProfile = pstmt.executeQuery();
                            if (rsProfile.next()) {
                                payoutsbalance = rsProfile.getDouble("payoutsbalance");
                                unpayoutsbalance = rsProfile.getDouble("unpayoutsbalance");
                            }

                            double var = bondInc - orderBalanceAdd;

                            System.out.println("var : " + var + " bondInc : " + bondInc + "orderBalanceAdd : " + orderBalanceAdd);

                            if (var >= 0) {
                                //扣除保证金比 结算报酬多

                                double publishunpayoutsbalance = rs.getDouble("publishunpayoutsbalance");
                                double publishpayoutsbalance = rs.getDouble("publishpayoutsbalance");

                                payoutsbalance += (publishpayoutsbalance + var);
                                unpayoutsbalance += publishunpayoutsbalance;

                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("phone") + "\",\"订单取消成功\",'{\"msg\":\"消息内容：编号为的订单取消成功，取消结果: 订单补偿" + Tools.doubleFormat(var) + "元，如有问题请联系在线客服。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();

                                balance = payoutsbalance + unpayoutsbalance;
                                pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis()) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=" + Tools.doubleFormat(publishpayoutsbalance + publishunpayoutsbalance + var) + ",note=\"订单结算-订单取消-" + bean.getString("ordernum") + "\" where note=\"冻结-创建订单" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();

                            }
                            else if (var < 0) {

                                double publishunpayoutsbalance = rs.getDouble("publishunpayoutsbalance");
                                double publishpayoutsbalance = rs.getDouble("publishpayoutsbalance");

                                publishunpayoutsbalance += var;
                                if (publishunpayoutsbalance < 0) {
                                    publishpayoutsbalance += publishunpayoutsbalance;
                                    publishunpayoutsbalance = 0;
                                }
                                payoutsbalance += publishpayoutsbalance;
                                unpayoutsbalance += publishunpayoutsbalance;
                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("phone") + "\",\"订单取消成功\",'{\"msg\":\"消息内容：编号为的订单取消成功，取消结果: 订单结算" + Tools.doubleFormat(var) + "元，如有问题请联系在线客服。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();
                                balance = payoutsbalance + unpayoutsbalance;
                                pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis()) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=" + Tools.doubleFormat(publishpayoutsbalance + publishunpayoutsbalance) + ",note=\"订单结算-订单取消-" + bean.getString("ordernum") + "\" where note=\"冻结-创建订单" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();
                            }

                            pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=" + Tools.doubleFormat(payoutsbalance) + ",unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + rs.getString("phone") + "\"");
                            pstmt.executeUpdate();


                            //修改接单者  orderBalanceAdd 总是 >= 0 所以 bondInc 总是 <= bond
                            pstmt = conn.prepareStatement("select * from profileinfo where phone=\"" + rs.getString("receivephone") + "\"");
                            rsProfile = pstmt.executeQuery();
                            if (rsProfile.next()) {
                                payoutsbalance = rsProfile.getDouble("payoutsbalance");
                                unpayoutsbalance = rsProfile.getDouble("unpayoutsbalance");
                            }

                            var = orderBalanceAdd - bondInc;
                            System.out.println("var : " + var + " bondInc : " + bondInc + "orderBalanceAdd : " + orderBalanceAdd);


                            if (var >= 0) {
                                double receivebondunpayoutsbalance = rs.getDouble("receivebondunpayoutsbalance");
                                double receivebondpayoutsbalance = rs.getDouble("receivebondpayoutsbalance");

                                payoutsbalance += (receivebondpayoutsbalance + var);
                                unpayoutsbalance += receivebondunpayoutsbalance;
                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("receivephone") + "\",\"订单取消成功\",'{\"msg\":\"消息内容：编号为的订单取消成功，取消结果: 订单补偿" + Tools.doubleFormat(var) + "元，如有问题请联系在线客服。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();


                                balance = payoutsbalance + unpayoutsbalance;
                                pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis()) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=" + Tools.doubleFormat(receivebondpayoutsbalance + receivebondunpayoutsbalance + var) + ",note=\"订单结算-订单取消-" + bean.getString("ordernum") + "\" where note=\"冻结保证金" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();
                            }
                            else if (var < 0) {
                                double receivebondunpayoutsbalance = rs.getDouble("receivebondunpayoutsbalance");
                                double receivebondpayoutsbalance = rs.getDouble("receivebondpayoutsbalance");

                                receivebondunpayoutsbalance += var;
                                if (receivebondunpayoutsbalance < 0) {
                                    receivebondpayoutsbalance += receivebondunpayoutsbalance;
                                    receivebondunpayoutsbalance = 0;
                                }
                                payoutsbalance += receivebondpayoutsbalance;
                                unpayoutsbalance += receivebondunpayoutsbalance;

                                pstmt = conn.prepareStatement("insert into sysnotify(timestamp,rand, phone, title, text, time, status) values (" + System.currentTimeMillis() + ",\"" + Tools.getOrderNum() + "\",\"" + rs.getString("receivephone") + "\",\"订单取消成功\",'{\"msg\":\"消息内容：编号为的订单取消成功，取消结果: 订单结算" + Tools.doubleFormat(var) + "元，如有问题请联系在线客服。\"}',\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\",\"未读\")");
                                pstmt.executeUpdate();
                                balance = payoutsbalance + unpayoutsbalance;
                                pstmt = conn.prepareStatement("update balancemonitor set timestamp=" + (System.currentTimeMillis()) + ",currentbalance=" + Tools.doubleFormat(balance) + ",var=" + Tools.doubleFormat(receivebondpayoutsbalance + receivebondunpayoutsbalance) + ",note=\"订单结算-订单取消-" + bean.getString("ordernum") + "\" where note=\"冻结保证金" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();
                            }

                            pstmt = conn.prepareStatement("update profileinfo set payoutsbalance=" + Tools.doubleFormat(payoutsbalance) + ",unpayoutsbalance=" + Tools.doubleFormat(unpayoutsbalance) + " where phone=\"" + rs.getString("receivephone") + "\"");
                            pstmt.executeUpdate();

                            balance = payoutsbalance + unpayoutsbalance;
                            pstmt = conn.prepareStatement("insert into balancemonitor(rand,currentbalance,timestamp,phone, var, note, time) values(\"" + Tools.getOrderNum() + "\"," + Tools.doubleFormat(balance) + "," + System.currentTimeMillis() + ",\"" + rs.getString("receivephone") + "\"," + Tools.doubleFormat(var) + ",\"订单取消-订单结算" + bean.getString("ordernum") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
                            pstmt.executeUpdate();

                            pstmt = conn.prepareStatement("update orderlist set runningstatus=\"正常\",status=\"已取消\" where ordernum=\"" + bean.getString("ordernum") + "\"");
                            pstmt.executeUpdate();

                            pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + bean.getString("ordernum") + "\"");
                            ResultSet rsOrder = pstmt.executeQuery();
                            if (rsOrder.next()) {
                                JSONObject exception = JSON.parseObject(rsOrder.getString("exception"));
                                exception.put("finishDesc", bean.getString("finishDesc"));
                                pstmt = conn.prepareStatement("update orderlist set exception='" + exception.toJSONString() + "' where ordernum=\"" + bean.getString("ordernum") + "\"");
                                pstmt.executeUpdate();
                            }

                            pstmt = conn.prepareStatement("update exceptionslist set exceptionstatus=\"已解决\" where  ordernum=\"" + bean.getString("ordernum") + "\"");
                            pstmt.executeUpdate();
                            conn.commit();
                            msgToClient.put("result", "成功");


                        }
                    }
                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (SQLException ex) {

                    }
                }

                try {
                    List<Map<String, Object>> reviews = new ArrayList<>();

                    pstmt = conn.prepareStatement("select * from exceptionslist where exceptionstatus=\"待审核\"  ");
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        Map<String, Object> item = new JSONObject();
                        item = new JSONObject(Tools.getResultMap(rs));

                        pstmt = conn.prepareStatement("select * from orderlist where ordernum=\"" + rs.getString("ordernum") + "\"");
                        ResultSet rsOrder = pstmt.executeQuery();
                        if (rsOrder.next()) {
                            item.put("type", rsOrder.getString("type"));
                            item.put("exception", rsOrder.getString("exception"));
                            item.put("exceptionback", rsOrder.getString("exceptionback"));
                        }

                        reviews.add(item);
                    }
                    msgToClient.put("status", 200);
                    msgToClient.put("data", reviews);

                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "banned")) {
                try {

                    msgToClient.put("result", "失败");

                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("update profileinfo set banned=1 where phone=\"" + bean.getString("phone") + "\"");
                    pstmt.executeUpdate();

                    conn.commit();

                    msgToClient.put("result", "成功");

                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (SQLException ex) {

                    }
                }

                List<Map<String, Object>> users = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from profileinfo");
                    rs = pstmt.executeQuery();
                    users = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", users);
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "unbanned")) {
                try {

                    msgToClient.put("result", "失败");

                    conn.setAutoCommit(false);
                    pstmt = conn.prepareStatement("update profileinfo set banned=0 where phone=\"" + bean.getString("phone") + "\"");
                    pstmt.executeUpdate();

                    conn.commit();

                    msgToClient.put("result", "成功");

                } catch (Throwable e) {
                    try {
                        conn.rollback();
                    } catch (SQLException ex) {

                    }
                }

                List<Map<String, Object>> users = new ArrayList<>();
                try {
                    pstmt = conn.prepareStatement("select * from profileinfo");
                    rs = pstmt.executeQuery();
                    users = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", users);
                } catch (Throwable e) {
                    ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
                }
            }
            else if (Objects.equals(bean.getString("name"), "safetylist")) {
                try {
                    List<Map<String, Object>> safetylist = new ArrayList<>();
                    pstmt = conn.prepareStatement("select * from safetylist");
                    rs = pstmt.executeQuery();
                    safetylist = Tools.convertList(rs);
                    msgToClient.put("status", 200);
                    msgToClient.put("data", safetylist);
                } catch (Throwable e) {

                }
            }
            else if (Objects.equals(bean.getString("name"), "indexPage")) {
                try {

                    pstmt = conn.prepareStatement("select count(*) from profileinfo;");
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        msgToClient.put("totalCount", rs.getInt(1));
                    }

                    pstmt = conn.prepareStatement("select * from profileinfo");
                    rs = pstmt.executeQuery();
                    int todayCount = 0;
                    while (rs.next()) {
                        if (Tools.getSDF().parse(rs.getString("registertime").replace("T", " ")).getTime() > Tools.getDayStartTime(System.currentTimeMillis()) && Tools.getSDF().parse(rs.getString("registertime").replace("T", " ")).getTime() < Tools.getDayEndTime(System.currentTimeMillis())) {
                            todayCount++;
                        }
                    }

                    msgToClient.put("todayCount", todayCount);

                    msgToClient.put("status", 200);
                } catch (Throwable e) {

                }
            }
        }


        try {
            if (conn != null) conn.close();
        } catch (
                Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
        System.out.println("发出:" + msgToClient);
        return msgToClient;
    }
}
